/**
 * Ingenius-Group S.A.S
 *
 * Project: SAU
 * File: ListIndicators2EvaluatePresenter.java
 * Date: 07/02/2011
 */

package com.ingenius.sau.client.presenter;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.data.BasePagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.ingenius.sau.client.AppController;
import com.ingenius.sau.client.Constants;
import com.ingenius.sau.client.event.EditIndicator2EvaluateEvent;
import com.ingenius.sau.client.event.ListAssignedInstrumentsEvent;
import com.ingenius.sau.client.service.CoreServiceAsync;
import com.ingenius.sau.client.view.Display;
import com.ingenius.sau.shared.model.core.Feature2Evaluate;
import com.ingenius.sau.shared.model.core.Indicator2Evaluate;
import com.ingenius.sau.shared.model.core.Scale;
import com.ingenius.sau.shared.util.filter.BeanFilter;
import com.ingenius.sau.shared.util.filter.Comparison;
import com.ingenius.sau.shared.util.filter.Filter;

/**
 * Class <strong>ListIndicators2EvaluatePresenter</strong>
 * 
 * @author Jesus David
 * @version 0.1
 */
public class ListIndicators2EvaluatePresenter implements
		ListPresenter<Indicator2Evaluate> {

	public interface ListIndicators2EvaluateDisplay extends Display {
		void reload();
	}

	private final HandlerManager eventBus;
	private final CoreServiceAsync rpcService;
	private final ListIndicators2EvaluateDisplay display;
	private String selectedIndicator2EvaluateId;

	/**
	 * 
	 * @param eventBus
	 * @param rpcService
	 * @param display
	 */
	public ListIndicators2EvaluatePresenter(HandlerManager eventBus,
			CoreServiceAsync rpcService, ListIndicators2EvaluateDisplay display) {

		this.eventBus = eventBus;
		this.rpcService = rpcService;
		this.display = display;

		display.setPresenter(this);
		selectedIndicator2EvaluateId = null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.client.mvp.presenters.ListPresenter#doAdd()
	 */
	@Override
	public void doAdd() {
		fireEvent(new EditIndicator2EvaluateEvent(null));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.client.mvp.presenters.ListPresenter#doEdit()
	 */
	@Override
	public void doEdit() {
		fireEvent(new EditIndicator2EvaluateEvent(selectedIndicator2EvaluateId));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.client.mvp.presenters.ListPresenter#doDelete()
	 */
	@Override
	public void doDelete() {
		// TODO i18n
		final String failedMessage = "Failed to delete indicator to evaluate";
		final String successMessage = "Indicator to evaluate deleted sucessfully";

		AppController.setStatusMessage("Deleting...");
		rpcService.deleteIndicator2Evaluate(selectedIndicator2EvaluateId,
				new AsyncCallback<Boolean>() {
					@Override
					public void onFailure(Throwable caught) {
						GWT.log(Constants.MESSAGE_FAILED_DELETE
								+ Indicator2Evaluate.class.getName(), caught);
						display.showInfo(failedMessage);
						hideStatusMessage();
					}

					@Override
					public void onSuccess(Boolean result) {
						if (result.booleanValue() == true) {
							display.showInfo(successMessage);
							hideStatusMessage();

						} else {
							onFailure(new Throwable("Database error"));
						}
					}

					void hideStatusMessage() {
						AppController.hideStatusMessage();
						display.reload();
					}
				});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.client.mvp.presenters.ListPresenter#goInto()
	 */
	@Override
	public void goInto() {
		AppController.setAttribute(Constants.CURRENT_INDICATOR2EVALUATE_ID,
				selectedIndicator2EvaluateId);
		fireEvent(new ListAssignedInstrumentsEvent());
		String processId = (String) AppController
				.getAttribute(Constants.CURRENT_EVALUATION_ID);
		rpcService.getSelfEvaluationScale(processId,
				new AsyncCallback<Scale>() {
					@Override
					public void onSuccess(Scale result) {
						AppController.setAttribute(Constants.COOKIE_SCALE,
								result);
					}

					@Override
					public void onFailure(Throwable caught) {
						System.out.println("Erroro loading evaluation scale. "
								+ caught.getMessage());
					}
				});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.client.presenter.ListPresenter#fireEvent()
	 */
	@Override
	public void fireEvent(GwtEvent<?> event) {
		eventBus.fireEvent(event);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ingenius.sau.client.mvp.presenters.ListPresenter#setSelectedItemId
	 * (java.lang.String)
	 */
	@Override
	public void setSelectedItemId(String id) {
		this.selectedIndicator2EvaluateId = id;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ingenius.sau.client.mvp.presenters.ListPresenter#loadData(java.util
	 * .List, java.lang.Object, com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void loadData(List<Filter> filters, Object loadConfig,
			AsyncCallback<PagingLoadResult<Indicator2Evaluate>> callback) {

		String currentFeature2Evaluate = (String) AppController
				.getAttribute(Constants.CURRENT_FEATURE2EVALUATE_ID);
		if (currentFeature2Evaluate != null) {
			filters = (filters == null) ? filters = new ArrayList<Filter>()
					: filters;
			filters.add(new BeanFilter(Comparison.EQUALS, "feature2Evaluate",
					new Feature2Evaluate(currentFeature2Evaluate)));
		}
		rpcService.findIndicators2Evaluate(filters,
				(BasePagingLoadConfig) loadConfig, callback);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ingenius.sau.client.mvp.presenters.Presenter#go(com.google.gwt.user
	 * .client.ui.HasWidgets)
	 */
	@Override
	public void go(HasWidgets container) {
		container.clear();
		container.add(display.asWidget());
	}

	@Override
	public String getModuleName() {
		return Constants.TOKEN_PROCESSES;
	}

}
